package type.pets;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 * @Date 2021/4/29 3:06 下午
 * @Author dengxin
 */
public abstract class PetsCreator {

    SecureRandom random = new SecureRandom();

    abstract List<Class<? extends Pet>> types();

    Pet createRandomPet() {
        Class<? extends Pet> clazz = types().get(random.nextInt(types().size()));
        try {
            return clazz.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            System.out.println("Pet instance error");
            e.printStackTrace();
            return null;
        }
    }

    Pet[] createArray(int size) {
        Pet[] petArray = new Pet[size];
        for (int i = 0; i < size; i++) {
            petArray[i] = createRandomPet();
        }
        return petArray;
    }

    List<Pet> createPetList(int size) {
        List<Pet> petList = new ArrayList<>(size);
        Collections.addAll(petList, createArray(size));
        return petList;
    }
}
